﻿using GN.Pay.TradeChannel.DefaultImpl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.YJPay.Response
{
    /// <summary>
    /// 退款通知实体
    /// </summary>
    [Serializable]
    class RefundNotifyEntity : NotifyEntity
    {
        /// <summary>
        /// 交易号
        /// </summary>
        public string tradeNo { get; set; }

        /// <summary>
        /// 退款完成时间
        /// </summary>
        public string refundFinishTime { get; set; }

        /// <summary>
        /// 实际退款金额
        /// </summary>
        public decimal refundAmount { get; set; }

        /// <summary>
        /// 退款状态
        /// </summary>
        public string refundStatus { get; set; }

        /// <summary>
        /// 交易是否成功
        /// </summary>
        /// <returns></returns>

        public bool IsSuccess()
        {
            return
                "EXECUTE_SUCCESS".Equals(resultCode, StringComparison.CurrentCulture);
        }

        public ITradeRefundApplyResult ToResult(IChannel channel)
        {
            if (string.IsNullOrEmpty(this.orderNo))
            {
                channel.ThrowChannelValidationException(this.orderNo + "易极付通知商户订单号为空或者空白！");
            }
            if (string.IsNullOrEmpty(this.tradeNo))
            {
                channel.ThrowChannelValidationException(this.partnerId + "易极付通知商户交易流水号为空或者空白！");
            }
            if (this.refundAmount <= 0)
            {
                channel.ThrowChannelValidationException("易极付退款通知金额小于零！");
            }
            if (this.IsSuccess())
            {
                return new TradeRefundApplyResult(channel, this.orderNo, tradeNo, refundAmount, 0m, null, TradeApplyResultState.Complete_Success, "成功");
            }
            return new TradeRefundApplyResult(channel, this.orderNo, tradeNo, refundAmount, 0m, null, TradeApplyResultState.Apply_Error, this.resultMessage);
        }
    }
}
